﻿// Инициализация и настройка дерева и списка преподавателей
$(function () {
    $("#teacherList").buttonset();
    initDististributionTree();
    initNotDististributionTree();
    initTeacherList();
    initDivideAtomsDialog();
    initDivideTree();
});


function initDivideAtomsDialog()
{
        $("#dialog:ui-dialog").dialog("destroy");
        $("#dialog_DivideAtoms").dialog({
            autoOpen: false,
            height: 550,
            width: 1000,
            modal: true,
            buttons: {
                "Разделить атом": function () {                
                    if ($("#divideParams").css("display") == "none")
                    {
                        alert("Выберите атом для разделения!");                        
                    }
                    else 
                    {
                        var firstAtomHours = $("#div_value1").val();
                        var secondAtomHours = $("#div_value2").attr("value");
                        var atomId = $("#atomID").val();                        
                        DivideAtom(firstAtomHours, secondAtomHours, atomId);
                        $("#divideParams").css("display", "inherit");
                        $(this).dialog("close");
                    }
                },
                "Отмена": function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#btn_divideLoadAtom")
			.button()
            .click(function () {
            UpdateDivideTree();                 
            $("#dialog_DivideAtoms").dialog("open");                                
        });
}

function initDististributionTree() {
    // Привязка удаления к dblClick
    $("li[id^='distrLoadAtom']").dblclick(function () {
        var atomId = $(this).attr('name');
        deleteDistrubAtom(atomId);        
        return false;
    });
    $("li[id^='distrLoadType']").dblclick(function () {        
        var paramArray = $(this).attr("name").split('_');
        deleteDistrubLoadType(paramArray[0], paramArray[1]);        
        return false;
    });
    $("li[id^='distrDiscipline']").dblclick(function () {
        var disciplineId = $(this).attr("name");
        deleteDistrubLoadDiscipline(disciplineId);
        return false;
    });
    // Собственно инициализация
    $("#dististributionLoadTree")
        .jstree({
            "core": {
               // "initially_open": ["teacher", "distrDiscipline", "distrLoadType"],
                "initially_open": ["teacher", "distrDiscipline"],
                "animation": 250
            },
            "themes": {
                "theme": "default",
                "dots": true,
                "icons": true
            },
            "types": {
                "valid_children": ["teacher"],
                "types": {
                    "teacher": {
                        "icon": {
                            "image": "../Content/icons/user.png"
                        }
                    },
                    "distrDiscipline": {
                        "icon": {
                            "image": "../Content/icons/book_up.png"
                        }
                    },
                    "distrLoadType": {
                        "icon": {
                            "image": "../Content/icons/book_edit.png"
                        }
                    },
                    "distrLoadAtom": {
                        "icon": {
                            "image": "../Content/icons/business_users.png"
                        }
                    },
                    "default": {
                        "valid_children": ["default"]
                    }
                }
            },
            "ui": {
                "select_limit": 0
            },
            "plugins": ["themes", "html_data", "types", "ui"]
        });
}

function initNotDististributionTree() {
    // Привязка распределения к dblClick
    $("li[id^='loadAtom']").dblclick(function () {
        distributeLoad($(this));
        return false;
    });
    $("li[id^='loadType']").dblclick(function () {
        distributeLoad($(this));
        return false;
    });
    $("li[id^='discipline']").dblclick(function () {
        distributeLoad($(this));
        return false;
    });
    $("#notDististributionLoadTree")
        .jstree({
            "core": {
                "initially_open": ["root"],
                "animation": 250
            },
            "dnd": {
                "drop_check": function (data) {
                    if (data.o.attr("id") == "root")
                        return false;
                    return true;
                },
                "drop_finish": function (data) {
                    distributeLoad(data.o);
                    return true;
                },
                "open_timeout" : 0,
                "check_timeout" : 0
            },
            "crrm": {
                "move": {
                    "check_move": function (m) {
                        return false;
                    }
                }
            },
            "themes": {
                "theme": "default",
                "dots": true,
                "icons": true
            },
            "types": {
                "valid_children": ["root"],
                "types": {
                    "root": {
                        "icon": {
                            "image": "../Content/icons/process_help.png"
                        }
                    },
                    "discipline": {
                        "icon": {
                            "image": "../Content/icons/book_help.png"
                        }
                    },
                    "loadType": {
                        "icon": {
                            "image": "../Content/icons/book_edit.png"
                        }
                    },
                    "loadAtom": {
                        "icon": {
                            "image": "../Content/icons/business_users.png"
                        }
                    },
                    "default": {
                        "valid_children": ["default"]
                    }
                }
            },
            "ui": {
                "select_limit": 0,                
            },
            "cookies": {
                "auto_save": true,
                "save_opened": "jstree_open"
            },
            "plugins": ["themes", "html_data", "dnd", "crrm", "types", "cookies", "ui"]
        });
}

function initDivideTree() {
    // Привязка распределения к dblClick
    $("li[id^='div_loadAtom']").dblclick(function () {
               
        $("#divideParams").css("display", "inherit");
        var paramArray = $(this).attr("name").split('_');
        var hours =  paramArray[1];
        var value1 = hours/2;        
        if ((hours % 2) != 0)
        {
            value1 = hours/2 + 0.5;
        }
        var test =  $("#div_value1").val();
        $("#div_value1").val(value1);
        $("#div_value2").attr("value", hours - value1);
        $("#atomID").val(paramArray[0]);
        return false;
    });
    $("li[id^='div_loadType']").dblclick(function () {
        alert('Разделить можно только атом!');
        $("#divideParams").css("display", "none");
        return false;
    });
    $("li[id^='div_discipline']").dblclick(function () {
        alert('Разделить можно только атом!');
        $("#divideParams").css("display", "none");
        return false;
    });
    $("#LoadTreeForDivide")
        .jstree({
            "core": {
                "initially_open": ["div_root"],
                "animation": 0
            },
            "dnd": {
                "drop_check": function (data) {
                    if (data.o.attr("id") == "div_root")
                        return false;
                    return true;
                },
                "drop_finish": function (data) {
                    distributeLoad(data.o);
                    return true;
                },
                "open_timeout" : 0,
                "check_timeout" : 0
            },
            "crrm": {
                "move": {
                    "check_move": function (m) {
                        return false;
                    }
                }
            },
            "themes": {
                "theme": "default",
                "dots": true,
                "icons": true
            },
            "types": {
                "valid_children": ["div_root"],
                "types": {
                    "div_root": {
                        "icon": {
                            "image": "../Content/icons/process_help.png"
                        }
                    },
                    "div_discipline": {
                        "icon": {
                            "image": "../Content/icons/book_help.png"
                        }
                    },
                    "div_loadType": {
                        "icon": {
                            "image": "../Content/icons/book_edit.png"
                        }
                    },
                    "div_loadAtom": {
                        "icon": {
                            "image": "../Content/icons/business_users.png"
                        }
                    },
                    "default": {
                        "valid_children": ["default"]
                    }
                }
            },
            "ui": {
                "select_limit": 1,                           
            },
            "plugins": ["themes", "html_data", "dnd", "crrm", "types","ui"]
        });
}
